package com.jingan.nursinghome.regulatory.dialog;

import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.adapter.MicrometerTableAdapter;
import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.MySpacesItemDecoration;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.AlertDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.utils.UtilToast;

/**
 * Created by 彭治铭 on 2017/10/12.
 */

public class MicrometerTableOSDialog extends BaseDialog {
    public static int pageIndex = 1;//当前页数。从1开始。题号。
    public static int count = 3;
    private static int maxPage;//最大页

    public static int getMaxPage() {
        maxPage = (int) Math.ceil((double) MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                .jlyName).size() / (double) (count - 1));
        return maxPage;
    }

    private MicrometerTableOSDialog(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_micrometertablleos, false, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
        BaseApplication.getInstance().setStatusBarDrak(window, true);
        initUI();
        adapterUI();
    }

    public static MicrometerTableOSDialog micrometerTableOSDialog;

    public static MicrometerTableOSDialog getInstance(BaseActivity baseActivity) {
        if (isFinishing(micrometerTableOSDialog)) {
            micrometerTableOSDialog = new MicrometerTableOSDialog(baseActivity);
        } else {
            micrometerTableOSDialog.show();
        }
        return micrometerTableOSDialog;
    }

    MyActionBar myActionBar;

    TextView txt_hostory, txt_select, txt_next, txt_next2;

    RecyclerView recyclerView;

    public String tihao = "";


    TextView txt_hot;

    void initHot() {
        if (txt_hot == null) {
            txt_hot = (TextView) findViewById(R.id.txt_hot);
            ProportionUtils.getInstance().adapterTextView(txt_hot);
        }
        if (MicrometerTable.getInstance().getHostoryQFTopics().size() > 0) {
            txt_hot.setVisibility(View.VISIBLE);
            txt_hot.setText(MicrometerTable.getInstance().getHostoryQFTopics().size() + "");
        } else {
            txt_hot.setVisibility(View.INVISIBLE);
        }
    }

    void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "千分评分系统");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                onBack();
            }
        });
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    onBack();
                    return true;//记得返回true。表示已经处理。
                }
                return false;//返回键以外交给系统自行处理。不可以屏蔽，不然输入法键盘的按键可能无效。如删除键
            }
        });
        txt_hostory = (TextView) findViewById(R.id.txt_hostory);
        txt_select = (TextView) findViewById(R.id.txt_select);
        txt_next = (TextView) findViewById(R.id.txt_next);
        txt_next2 = (TextView) findViewById(R.id.txt_next2);//這個隱藏。就有历史整改
        txt_next2.setVisibility(View.INVISIBLE);
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.txt_hostory://历史整改
                        if (MicrometerTable.getInstance().getHostoryQFTopics().size() > 0) {
                            HostoryDialog.getInstance(baseActivity, MicrometerTable.getInstance().getHostoryQFTopics());
                        } else {
                            UtilToast.showToastView("暂无历史整改");
                        }
                        break;
                    case R.id.txt_select://选题
                        XuantiDialog.getInstance(baseActivity, new BaseListenner<Integer>() {
                            @Override
                            public void onResult(Integer result) {
                                //返回当前页面
                                pageIndex = result;
                                //recyclerView.smoothScrollToPosition(pageIndex);
                                //linearLayoutManager.scrollToPosition(pageIndex);
                                linearLayoutManager.scrollToPositionWithOffset(pageIndex - 1, 0);
                            }
                        });
                        break;
                    case R.id.txt_next://下一题
                    case R.id.txt_next2:

                        if (pageIndex >= MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                .jlyName).size() || linearLayoutManager.findLastCompletelyVisibleItemPosition() == (MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                .jlyName).size() - 1)) {
                            //最后一页
                            int score = 0;//得分。
                            int maxScore = 0;//总分
                            tihao = "";

                            pageIndex = MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                    .jlyName).size();

                            //当前题目的得分和总分
                            for (int i = 0; i < MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                    .jlyName).size(); i++) {
                                maxScore = maxScore + Integer.valueOf(MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                        .jlyName).get(i).getMaxScore());
                                String c = MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                        .jlyName).get(i).getCscore();
                                if (c != null && !c.trim().equals("")) {
                                    score = score + Integer.valueOf(c);
                                }
                            }
                            //历史整改的得分和总分
                            if (MicrometerTable.getInstance().getHostoryQFTopics().size() > 0) {
                                for (int i = 0; i < MicrometerTable.getInstance().getHostoryQFTopics().size(); i++) {
                                    maxScore = maxScore + Integer.valueOf(MicrometerTable.getInstance().getHostoryQFTopics().get(i).getMaxScore());
                                    String c = MicrometerTable.getInstance().getHostoryQFTopics().get(i).getCscore();
                                    if (c != null && !c.trim().equals("")) {
                                        score = score + Integer.valueOf(c);
                                    }
                                }
                            }

//                            for (int i = 0; i < pageIndex; i++) {
                            for (int i = 0; i < MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                    .jlyName).size(); i++) {

//                                maxScore = maxScore + Integer.valueOf(MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
//                                        .jlyName).get(i).getMaxScore());
                                String num = MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                        .jlyName).get(i).getCount();
                                String c = MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                        .jlyName).get(i).getCscore();
                                if (c == null || c.equals("")) {
                                    if (tihao.equals("")) {
                                        tihao = num;
                                    } else {
                                        tihao = tihao + "," + num;
                                    }
                                }
//                                else {
//                                    score = score + Integer.valueOf(c);
//                                }
                            }

                            //跳转到提交页
                            CommitDailog.getInstance(baseActivity, micrometerTableOSDialog, maxScore + "", score + "");

                            //if (tihao.equals("")||true) {//true为测试
//                            if (tihao.equals("")) {
//                                //跳转到提交页
//                                CommitDailog.getInstance(baseActivity, micrometerTableOSDialog, maxScore + "", score + "");
//                            } else {
//                                baseActivity.SnackbarMake("第" + tihao + "题还没有评分哦!", true, recyclerView);
//                            }
                            break;
                        }

                        pageIndex += 2;
                        if (pageIndex >= MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                .jlyName).size()) {
                            pageIndex = MicrometerTable.getInstance().getTopics(CreateMicrometerTableDailog.rid, CreateMicrometerTableDailog
                                    .jlyName).size();
                        }
                        canScrollVertically = true;
                        recyclerView.smoothScrollToPosition(pageIndex);
                        break;
                }
            }
        };
        txt_hostory.setOnClickListener(onClickListener);
        txt_select.setOnClickListener(onClickListener);
        txt_next.setOnClickListener(onClickListener);
        txt_next2.setOnClickListener(onClickListener);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity) {
            @Override
            public boolean canScrollVertically() {
                //return canScrollVertically;
                return true;
            }
        };
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new MySpacesItemDecoration(baseActivity, 0));
        //recyclerView.setAdapter(new MicrometerTableAdapter(baseActivity));

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                    //int postion = linearLayoutManager.findFirstVisibleItemPosition();
                    int postion = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                    pageIndex = postion + 1;
                    linearLayoutManager.scrollToPositionWithOffset(pageIndex - 1, 0);
                    canScrollVertically = false;
                }
            }
        });
        recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    canScrollVertically = false;
                }
                return false;
            }
        });

    }

    boolean canScrollVertically = false;
    LinearLayoutManager linearLayoutManager;

    AlertDialog alertDialog;

    void onBack() {
        alertDialog = AlertDialog.getInstance(baseActivity, false, true).setMession("是否退出本次评分?");
        alertDialog.setPositive("是", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                alertDialog.dismiss();
            }
        });
    }

    void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_micrometertableos_constraintLayout));
        ProportionUtils.getInstance().adapterTextView(txt_hostory);
        ProportionUtils.getInstance().adapterTextView(txt_select);
        ProportionUtils.getInstance().adapterTextView(txt_next);
        ProportionUtils.getInstance().adapterTextView(txt_next2);
    }

    @Override
    protected void listener() {
        pageIndex = 1;
        recyclerView.setAdapter(new MicrometerTableAdapter(baseActivity));
        initHot();
    }

    //更新
    public void updateAdapter() {
        recyclerView.setAdapter(new MicrometerTableAdapter(baseActivity));
    }

    @Override
    protected void recycleView() {

    }
}
